#include "cxmonsetdialog.h"
#include "ui_cxmonsetdialog.h"
#include "cxglobal.h"
#ifndef Q_OS_WIN
    #include <strstream>
#endif
#include <boost/xpressive/xpressive.hpp>


CxMonSetDialog::CxMonSetDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CxMonSetDialog)
{
    ui->setupUi(this);
    setParam();
}

CxMonSetDialog::~CxMonSetDialog()
{
    delete ui;
}


bool CxMonSetDialog::isIPAddr(const QString str)
{
    boost::xpressive::cregex re=boost::xpressive::cregex::compile("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\." \
                                                                  "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\." \
                                                                  "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\."   \
                                                                  "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");

  if(!boost::xpressive::regex_match(str.toStdString().c_str(),re))
   return false;
   else return true;
}

void    CxMonSetDialog::setParam()
{
    CxGlobal  gl;

    ui->lineEdit_ip1->setText(gl.getIpaddr(0).c_str());
    ui->lineEdit_ip2->setText(gl.getIpaddr(1).c_str());
    ui->lineEdit_ip3->setText(gl.getIpaddr(2).c_str());
    ui->lineEdit_ip4->setText(gl.getIpaddr(3).c_str());


    char  buffer[16];

#ifndef Q_OS_WIN
    std::strstream  stream;
    stream<<gl.getPortnumber(0);
    stream>>buffer;
#else
    sprintf(buffer,"%d",gl.getPortnumber(0));
#endif

    ui->lineEdit_port1->setText(buffer);


#ifndef Q_OS_WIN
    stream.flush();
    stream<<gl.getPortnumber(1);
    stream>>buffer;
#else
    sprintf(buffer,"%d",gl.getPortnumber(1));
#endif
    ui->lineEdit_port2->setText(buffer);


#ifndef Q_OS_WIN
    stream.flush();
    stream<<gl.getPortnumber(2);
    stream>>buffer;
#else
    sprintf(buffer,"%d",gl.getPortnumber(2));
#endif

    ui->lineEdit_port3->setText(buffer);


#ifndef Q_OS_WIN
    stream.flush();
    stream<<gl.getPortnumber(3);
    stream>>buffer;
#else
    sprintf(buffer,"%d",gl.getPortnumber(3));
#endif
    ui->lineEdit_port4->setText(buffer);
}


void CxMonSetDialog::on_buttonBox_accepted()
{

    QString ipaddr[4];
    short   port[4];

    CxGlobal  gl;

    ipaddr[0]=ui->lineEdit_ip1->text();
    if(isIPAddr(ipaddr[0]))
        gl.setIpAddr(0,ipaddr[0].toStdString());

    ipaddr[1]=ui->lineEdit_ip2->text();
    if(isIPAddr(ipaddr[1]))
        gl.setIpAddr(1,ipaddr[1].toStdString());

    ipaddr[2]=ui->lineEdit_ip3->text();
    if(isIPAddr(ipaddr[2]))
        gl.setIpAddr(2,ipaddr[2].toStdString());

    ipaddr[3]=ui->lineEdit_ip4->text();
    if(isIPAddr(ipaddr[3]))
        gl.setIpAddr(3,ipaddr[3].toStdString());

    if(ui->lineEdit_port1->text()!="")
        gl.setPortnumber(0,ui->lineEdit_port1->text().toShort());

    if(ui->lineEdit_port2->text()!="")
        gl.setPortnumber(1,ui->lineEdit_port1->text().toShort());

    if(ui->lineEdit_port3->text()=="")
        gl.setPortnumber(2,ui->lineEdit_port1->text().toShort());

    if(ui->lineEdit_port4->text()=="")
        gl.setPortnumber(3,ui->lineEdit_port1->text().toShort());

}
